package com.home766.util;


import com.home766.dto.PaymentDTO;

import java.io.*;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class HttpClient{
	private static final String DEFAULT_CHARSET="UTF-8";
	/**
	 * 模拟HTTP请求
	 * @throws IOException
	 */
	public static void post() throws IOException {
		URL url = new URL("http://120.27.147.173/766/");
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);   
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "8859_1");
        out.write("username=kevin&password=*********"); //向页面传递数据。
     // remember to clean up     
        out.flush();     
        out.close();    
        String sCurrentLine;
        String sTotalString;
        sCurrentLine = "";     
        sTotalString = "";     
        InputStream l_urlStream;
        l_urlStream = connection.getInputStream();
        BufferedReader l_reader = new BufferedReader(new InputStreamReader(
                l_urlStream));     
        while ((sCurrentLine = l_reader.readLine()) != null) {     
            sTotalString += sCurrentLine + "/r/n";     
    
        }     
        System.out.println(sTotalString);
	}
	 public static String doPost(String address, PaymentDTO dto){
		 String params=getParams(dto);
		 URL url = null;
	        HttpURLConnection httpurlconnection = null;
	        StringBuilder result = new StringBuilder();
	        try {
	            url = new URL(address);
	            // 以post方式请求
	            httpurlconnection = (HttpURLConnection) url.openConnection();
	            httpurlconnection.setDoOutput(true);
	            httpurlconnection.setRequestMethod("POST");
	            if(null!=params&&params.length()>0){
	                httpurlconnection.getOutputStream().write(params.getBytes());
	                httpurlconnection.getOutputStream().flush();
	                httpurlconnection.getOutputStream().close();
	            }
	            // 获取页面内容
	            java.io.InputStream in = httpurlconnection.getInputStream();
	            java.io.BufferedReader breader = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));
	            String str = breader.readLine();
	            while (str != null) {
	                result.append(str);
	                str = breader.readLine();
	                System.out.println(str);
	            }
	            breader.close();
	            in.close();
	        } catch (Exception e) {
	             
	        } finally {
	            if (httpurlconnection != null)
	                httpurlconnection.disconnect();
	        }
	        return result.toString().trim();
	 }
	 private static String getParams(PaymentDTO dto){
		 Class clz=dto.getClass();
		 Field[] fields=clz.getDeclaredFields();
		 String content="";
		 try {
			 if(fields.length>1){
				 Field f=fields[0];
				 f.setAccessible(true);
				 String name=f.getName();
				 Object value=null;
				 value=f.get(dto);
				 content = name+"=" + String.valueOf(value);
			 }
			 for(int i=1;i<fields.length;i++){
				 Field f=fields[i];
				 f.setAccessible(true);
				 String name=f.getName();
				 System.out.println(name);
				 Object value=null;
				 value=f.get(dto);
				 content += "&"+name+"="+ String.valueOf(value); //+ URLEncoder.encode(String.valueOf(value), "UTF-8");
			 }
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 return content;
	 } 
}
